{extend name="$base" /}
{block name="resources"}
<link rel="stylesheet" type="text/css" href="ADMIN_CSS/member_list.css" />
<style>
.is-enabled{display: block;width:15px;cursor: pointer;}
.count-table .title{font-weight:bold;margin-bottom:10px;}
.count-table span{display:inline-block;width:16%;}
#add_virtual_goods{width: 800px;left: 45%;}
#add_virtual_goods span{line-height: 7px;display: block;margin-top: 8px;}
#add_virtual_goods textarea {width: 80% !important;height: 128px !important;}
</style>
{/block}
{block name="main"}
<table class="table-class count-table">
	<tbody>
		<tr>
			<td width="1%"></td>
			<td>
				<div class="title">商品信息</div>
				<span>商品名称：{$goods_info['goods_name']}</span>
				<span>销售价格：{$goods_info['price']}</span>
				<span>总库存：{$goods_info['stock']}</span>
			</td>
		</tr>
		<tr>
			<td width="1%"></td>
			<td>
				<div class="title">虚拟信息</div>
				<span>虚拟商品分组：{$virtual_group_info['virtual_goods_group_name']}</span>
				<span>有效期/天：{$virtual_goods_type_info['validity_period']}</span>
				<span>限制使用次数：{$virtual_goods_type_info['confine_use_number']}</span>
				{if condition = "$virtual_goods_type_info['virtual_goods_group_id'] eq 3"}
				<span>卡密库存：{$virtual_goods_count}</span>
				{/if}
			</td>
		</tr>
	</tbody>
</table>

<table class="mytable">
	<tr>
		<th style="text-align: left;width: 15%;">
			<!-- 只要是类型为卡密的，才可添加卡密 -->
			{if condition = "$virtual_goods_type_info['virtual_goods_group_id'] eq 3"}
			<button class="btn-common btn-small" onclick="addVirtualGoods()" style="margin:0 5px 0 0 !important;">添加卡密</button>
			{/if}
			<a class="btn-common-white" href="javascript:batchDelete()">批量删除</a>
		</th>
		
		<th>
			虚拟商品名称：
			<input type="text" id='search_name' placeholder="输入虚拟商品名称" class="input-common middle"/>
			虚拟商品编号：
			<input type="text" id='virtual_code' placeholder="输入虚拟商品编号" class="input-common middle"/>
			状态：
			<select id="use_status" class="select-common w100">
				<option value="" >全部</option>
				<option value="1">已使用</option>
				<option value="-1">已过期</option>
				<option value="0" >未使用</option>
				<option value="-2" >未发放</option>
			</select>
			<input type="button" onclick="LoadingInfo(1)" value="搜索" class="btn-common"/>
		</th>
	</tr>
</table>

<table class="table-class">
	<colgroup>
		<col style="width: 3%;">
		<col style="width: 9%;">
		<col style="width: 14%;">
		<col style="width: 12%;">
		<col style="width: 12%;">
		<col style="width: 12%;">
		<col style="width: 12%;">
		<col style="width: 15%;">
		<col style="width: 15%;">
	</colgroup>
	<thead>
		<tr>
			<th>
				<i class="checkbox-common">
					<input type="checkbox"  />
				</i>
			</th>
			<th align="left">商品名称</th>
			<th align="left">虚拟码</th>
			<th align="left">买家</th>
			<th>开始时间</th>
			<th>结束时间</th>
			<th>状态</th>
			<th align="left">说明</th>
			<th>操作</th>
		</tr>
	</thead>
	<tbody class="virtual_goods_list"></tbody>
</table>
<input type="hidden" id="goods_id" value="{$goods_id}"/>
	
<div class="modal fade hide" id="add_virtual_goods" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
	
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h3>添加卡密</h3>
			</div>
			<div class="modal-body">
				<div class="modal-infp-style">
					<table>
						<tbody>
							<tr>
								<td>添加卡密</td>
								<td>
									<span style="font-weight: bold;color: #FF5722;">导入格式为卡号+空格+密码(可跟上附加内容)，一行一组，如AAAAA BBBBB</span><br/>
									<textarea class="input-common" id="card_password"></textarea><span style="color: red;display: none;" id="card_msg">不可为空！</span>
								</td>
							</tr>
						</tbody>
					</table>
					
				</div>
			</div>
			<div class="modal-footer">
				<input type="hidden" id="point_id" value="5">
				<button class="btn-common" onclick="ajaxAddVirtualGoods()">保存</button>
				<button class="btn" data-dismiss="modal">关闭</button>
			</div>
		</div>
	</div>
	
</div>
{/block}
{block name="script"}
<script type="text/javascript">
var goods_id = '{$goods_id}';
var virtual_goods_type_id = '{$virtual_goods_type_info["virtual_goods_type_id"]}';

//添加卡密显示
function addVirtualGoods(){
	$('#card_msg').hide();
	$("#add_virtual_goods").modal("show");
}

//ajax添加卡密
function ajaxAddVirtualGoods(){
	var card_password = $("#card_password").val();
	if(card_password == ''){
		$('#card_msg').show();
		return false;
	}else{
		$('#card_msg').hide();
	}
	var cp_array = card_password.split("\n");
	var virtual_card = new Array();
	for(var i=0;i<cp_array.length;i++){
		
		if(cp_array[i]!=""){
			virtual_card.push({
				remark : cp_array[i]
			});
		}
	}
	$.ajax({
		type : "post",
		url : "{:__URL('ADMIN_MAIN/goods/ajaxAddVirtualGoods')}",
		data : {
			"virtual_card" : JSON.stringify(virtual_card),
			"goods_id" : goods_id,
			"virtual_goods_type_id" : virtual_goods_type_id
		},
		success : function(data) {
			
			if(data['code'] > 0){
				$("#add_virtual_goods").modal("hide");
				LoadingInfo($(".currentPage").text());
				showTip(data['message'], "success");
			}
		}
	})
}

//加载数据
function LoadingInfo(page_index) {
	var goods_id = $("#goods_id").val();
	var search_name = $("#search_name").val();
	$.ajax({
		type : "post",
		url : "{:__URL('ADMIN_MAIN/goods/getVirtualGoodsListByGoodsId')}",
		data : {
			"page_index" : page_index,
			"page_size" : $("#showNumber").val(),
			"search_name" : search_name,
			"use_status" : $("#use_status").val(),
			"virtual_code" : $("#virtual_code").val(),
			"goods_id" : goods_id
		},
		success : function(data) {
			
			if (data["data"].length > 0) {
				$(".virtual_goods_list").empty();
				for (var i = 0; i < data["data"].length; i++) {	
					var html = '';
					if( data["data"][i]["confine_use_number"] == 0){
						var number_info = "不限次数";
					}else{
						var number_info = data["data"][i]["confine_use_number"];
					}
					html += '<tr align="center">';
					html += '<td><i class="checkbox-common"><input name="sub" type="checkbox" value="'+ data["data"][i]["virtual_goods_id"] +'" ></i></td>';
					if(data["data"][i]["goods_name"] != null){
						html += '<td align="left">' + data["data"][i]["goods_name"] + '</td>';
					}else{
						html += '<td align="left">--</td>';
					}
					html += '<td align="left">'+data["data"][i]["virtual_code"]+'</td>';
					//html += '<td>'+data["data"][i]["use_number"]+'/'+ number_info +'</td>';

 					var row=1;//订单数量，用于设置跨行
 					if(data["data"][i]["nick_name"]){
 						html += '<td align="left">'+data["data"][i]["nick_name"]+'</td>';
 					}else{
 						html += '<td align="left">--</td>';
 					}
					
					if(data["data"][i]["start_time"] != 0){
						html += '<td>'+timeStampTurnTime(data["data"][i]["start_time"])+'</td>';
					}else{
						html += '<td>--</td>';
					}
					if(data["data"][i]["end_time"] != 0){
						html += '<td>'+timeStampTurnTime(data["data"][i]["end_time"])+'</td>';
					}else{
						html += '<td>--</td>';
					}
					
					if (data["data"][i]["use_status"] == - 1) {
						var use_status = '已过期';
					} else if (data["data"][i]["use_status"] == 0) {
						var use_status = '未使用';
					} else if (data["data"][i]["use_status"] == 1) {
						var use_status = '已使用';
					}else if (data["data"][i]["use_status"] == -2) {
						var use_status = '未发放';
					}
					html += '<td>'+use_status+'</td>';
					
					if(data["data"][i]["remark"]){
						html += '<td align="left">'+data["data"][i]["remark"]+'</td>';
					}else{
						html += '<td align="left">--</td>';
					}
					
					html += '<td>';
					if(data["data"][i]["use_status"] == 0){
						html += '<a href="javascript:verificationVirtualGoods(' + data["data"][i]["virtual_goods_id"] + ', ' + data["data"][i]["buyer_id"] + ')">核销</a>';
					}
					if(data["data"][i]["use_status"] == -2){
						html += '<a href="javascript:;" onclick="delete_virtual_goods('+ data["data"][i]["virtual_goods_id"] +')">删除</a>';
					}
					html +='</td></tr>';
					$(".virtual_goods_list").append(html);
				}
			} else {
				var html = '<tr align="center"><td colspan="10">暂无符合条件的数据记录</td></tr>';
				$(".virtual_goods_list").html(html);
			}
			initPageData(data["page_count"],data['data'].length,data['total_count']);
			$("#pageNumber").html(pagenumShow(jumpNumber,$("#page_count").val(),{$pageshow}));
		}
	});
}

//核销
function verificationVirtualGoods(virtual_goods_id,buyer_id){
	$( "#dialog" ).dialog({
		buttons: {
			"确定": function() {
				$.ajax({
					type : "post",
					url : "{:__URL('ADMIN_MAIN/verification/verificationVirtualGoods')}",
					data : { "virtual_goods_id" : virtual_goods_id.toString(),"buyer_id" : buyer_id },
					dataType : "json",
					success : function(data) {
						if(data["code"] > 0 ){
							$("#dialog").dialog({
								buttons: {
									"确定": function() {
										$(this).dialog('close');
									}
								},
								modal: true,
								contentText:data["message"],
								title:"消息提醒",
								time:1
							});
							LoadingInfo($(".currentPage").text());
							$("#chek_all").prop("checked", false);
						}else{
							$("#dialog").dialog({
								buttons: {
									"确定,#0059d6,#fff": function() {
										$(this).dialog('close');
									}
								},
								modal: true,
								contentText:data["message"],
								title:"消息提醒",
								time:1
							});
						}
					}
				});
				$(this).dialog('close');
			},
			"取消,#f5f5f5,#666": function() {
				$(this).dialog('close');
			},
		},
		contentText:"确定要此操作吗？",
	});
}

function batchDelete() {
	var virtual_goods_id= [];
	$(".virtual_goods_list input[type='checkbox']:checked").each(function() {
		if (!isNaN($(this).val())) {
			virtual_goods_id.push($(this).val());
		}
	});
	if(virtual_goods_id.length ==0){
		showTip("请选择需要操作的记录","warning");
		return false;
	}
	delete_virtual_goods(virtual_goods_id);
}

// 删除虚拟商品
function delete_virtual_goods(virtual_goods_id){
	$( "#dialog" ).dialog({
		buttons: {
			"确定": function() {
				$.ajax({
					type : "post",
					url : "{:__URL('ADMIN_MAIN/goods/deleteVirtualGoodsById')}",
					data : { "virtual_goods_id" : virtual_goods_id.toString()},
					dataType : "json",
					success : function(data) {
						if(data["code"] > 0 ){
							$("#dialog").dialog({
								buttons: {
									"确定": function() {
										$(this).dialog('close');
									}
								},
								modal: true,
								contentText:data["message"],
								title:"消息提醒",
								time:1
							});
							LoadingInfo(getCurrentIndex(virtual_goods_id,'.virtual_goods_list'));
							$("#chek_all").prop("checked", false);
						}else{
							$("#dialog").dialog({
								buttons: {
									"确定,#0059d6,#fff": function() {
										$(this).dialog('close');
									}
								},
								modal: true,
								contentText:data["message"],
								title:"消息提醒",
								time:1
							});
						}
					}
				});
				$(this).dialog('close');
			},
			"取消,#f5f5f5,#666": function() {
				$(this).dialog('close');
			},
		},
		contentText:"确定要删除吗？",
	});
}
</script>
{/block}